<?php
/*
Plugin Name: Whatsits
Plugin URI: http://whatsits.googlecode.com
Description: For inserting Google Gadgets as Wordpress Widgets
Version: 0.4
Author: Jacob Brunson
Author URI: http://jacobbrunson.com
*/

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/


function define_whatsits_config()
{
    DEFINE('WHATSIT_PATH', WP_PLUGIN_DIR.'/'.dirname(plugin_basename(__FILE__)).'/');
    DEFINE('WHATSIT_URL_PATH', WP_PLUGIN_URL.'/'.dirname(plugin_basename(__FILE__)).'/');
    include_once(WHATSIT_PATH.'/lib/lib.php');
    include_once(WHATSIT_PATH.'/lib/whatsit.class.php');
    include_once(WHATSIT_PATH.'/wplib/wpdb.class.php');
    include_once(WHATSIT_PATH.'/wplib/wpuser.class.php');
}

function widget_whatsit($args, $id_num)
{
    extract($args);

    define_whatsits_config();
    $database = new Wordpress_Database('default_username');
    $whatsit = $database->getWhatsit($id_num);

    echo $before_widget;
    echo $before_title . $whatsit->getTitle() . $after_title;
    $whatsit->draw();
    echo $after_widget;
}

function init_widget_whatsit()
{
    add_thickbox();

    $widget_whatsit_numbers = get_option('widget_whatsit_numbers');
    if (!is_array($widget_whatsit_numbers)) {
        register_sidebar_widget('Whatsit 1', 'widget_whatsit');
        register_widget_control('Whatsit 1', 'widget_whatsit_control', '', '', 1);
    } else {
        $next = 1;
        if (count($widget_whatsit_numbers) >= 1) {
            for ($i = 1; $i <= (max($widget_whatsit_numbers)+1); $i++) {
                if (!in_array($i, $widget_whatsit_numbers)) {
                    $next = $i;
                    break;
                }
            }
        }
        foreach ($widget_whatsit_numbers as $key => $num) {
            $num = (int)$num;
            //if (is_active_widget('widget_whatsit', sanitize_title("Whatsit $num"))) {
                error_log("Adding widget $num");
                register_sidebar_widget("Whatsit $num", 'widget_whatsit', $num);
                register_widget_control("Whatsit $num", 'widget_whatsit_control', '', '', $num);
            //} else {
            //    error_log("Unsetting widget $num");
            //    unset($widget_whatsit_numbers[$key]);
           // }
        }
        error_log("Adding default widget $next");
        register_sidebar_widget("Whatsit $next", 'widget_whatsit', $next);
        register_widget_control("Whatsit $next", 'widget_whatsit_control', '', '', $next);
    }
    update_option('widget_whatsit_numbers', $widget_whatsit_numbers);
}

add_action("plugins_loaded", "init_widget_whatsit");

function widget_whatsit_control($id_num)
{
    define_whatsits_config();
    $database = new Wordpress_Database('default_username');

    if ($_POST['whatsitControl-submit'][$id_num] == 1) {
        $widget_whatsit_numbers = get_option('widget_whatsit_numbers');
        if (!is_array($widget_whatsit_numbers)) {
            $widget_whatsit_numbers = array();
            update_option('widget_whatsit_numbers', $widget_whatsit_numbers);
        }
        if (!in_array($id_num, $widget_whatsit_numbers)) {
            $widget_whatsit_numbers[] = $id_num;
            update_option('widget_whatsit_numbers', $widget_whatsit_numbers);
        }
        $url = strip_tags(stripslashes($_POST['whatsit-url'][$id_num]));
        $database->setOption($id_num, 'url', $url);
    } 

    $whatsit = $database->getWhatsit($id_num);

    $url = htmlspecialchars($database->getOption($id_num, 'url'), ENT_QUOTES);
    echo "<p style='font-weight: bold;'>Gadget URL</p>";
    echo "<input style='width:200px;' name='whatsit-url[$id_num]' value='$url'/>";
    echo "<input type='hidden' name='whatsitControl-submit[$id_num]' value='1'/>";
    if ($whatsit->hasPreferences()) {
        $edit_ajax_url = WHATSIT_URL_PATH.'wplib/prefform-ajax-html.php?widget_id='. $id_num .'&KeepThis=true&TB_iframe=true';
        echo "<p><a href=\"$edit_ajax_url\" class=\"thickbox\" title=\"".$whatsit->getTitle()." Preferences\">Edit Preferences</a></p>";
    }
}
